package com.example.com.transcribeeditpublish.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.example.com.transcribeeditpublish.R;
import com.example.com.transcribeeditpublish.TCVideoEditerWrapper;
import com.example.com.transcribeeditpublish.utils.DialogUtil;
import com.example.com.transcribeeditpublish.utils.TCConstants;
import com.tencent.ugc.TXVideoEditConstants;
import com.tencent.ugc.TXVideoEditer;
import com.tencent.ugc.TXVideoInfoReader;

import java.lang.ref.WeakReference;

public class PrepareVideoInfoActivity extends FragmentActivity {
    private String mvideoPath;
    private TXVideoEditer mTXVideoEditer;
    private VideoMainHandler mVideoMainHandler;
    private Thread mLoadBackgroundThread;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prepare);
        mvideoPath = getIntent().getStringExtra(TCConstants.VIDEO_EDITER_PATH);
        initVideoEditer();
        // initPhoneListener();
        // 开始加载视频信息
        mVideoMainHandler = new VideoMainHandler(this);
        mLoadBackgroundThread = new Thread(new LoadVideoRunnable(this));
        mLoadBackgroundThread.start();
    }

    private void initVideoEditer() {

        mTXVideoEditer = new TXVideoEditer(this);
        int ret = mTXVideoEditer.setVideoPath(mvideoPath);
        if (ret != 0) {
            if (ret == TXVideoEditConstants.ERR_SOURCE_NO_TRACK) {
                DialogUtil.showDialog(this, "视频预处理失败", "不支持的视频格式", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
            } else if (ret == TXVideoEditConstants.ERR_UNSUPPORT_AUDIO_FORMAT) {
                DialogUtil.showDialog(this, "视频预处理失败", "暂不支持非单双声道的视频格式", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
            }
        }
        TCVideoEditerWrapper wrapper = TCVideoEditerWrapper.getInstance();
        wrapper.setEditer(mTXVideoEditer);
    }

    /**
     * 主线程的Handler 用于处理load 视频信息的完后的动作
     */
    private static class VideoMainHandler extends Handler {
        static final int LOAD_VIDEO_SUCCESS = 0;
        static final int LOAD_VIDEO_ERROR = -1;
        private WeakReference<PrepareVideoInfoActivity> mWefActivity;


        VideoMainHandler(PrepareVideoInfoActivity activity) {
            mWefActivity = new WeakReference<PrepareVideoInfoActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            PrepareVideoInfoActivity activity = mWefActivity.get();
            if (activity == null) return;
            switch (msg.what) {
                case LOAD_VIDEO_ERROR:
                    activity.loadVideoFail();
                    break;
                case LOAD_VIDEO_SUCCESS:
                    activity.loadVideoSuccess();
                    break;
            }
        }
    }



    private class LoadVideoRunnable implements Runnable {
        private WeakReference<PrepareVideoInfoActivity> mWekActivity;

        LoadVideoRunnable(PrepareVideoInfoActivity activity) {
            mWekActivity = new WeakReference<PrepareVideoInfoActivity>(activity);
        }

        @Override
        public void run() {
            if (mWekActivity == null || mWekActivity.get() == null) {
                return;
            }
            PrepareVideoInfoActivity activity = mWekActivity.get();
            if (activity == null) return;
            TXVideoEditConstants.TXVideoInfo info = TXVideoInfoReader.getInstance().getVideoFileInfo(activity.mvideoPath);
            if (info == null) {// error 发生错误
                activity.mVideoMainHandler.sendEmptyMessage(VideoMainHandler.LOAD_VIDEO_ERROR);
            } else {
                TCVideoEditerWrapper.getInstance().setTXVideoInfo(info);
                activity.mVideoMainHandler.sendEmptyMessage(VideoMainHandler.LOAD_VIDEO_SUCCESS);
            }
        }
    }

    private void loadVideoSuccess() {
        startProcess();
    }



    private void loadVideoFail() {
        DialogUtil.showDialog(this, "编辑失败", "暂不支持Android 4.3以下的系统", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void startProcess() {
       // Intent intent = new Intent(this, VideoSimpleActivity.class);
        Intent intent = new Intent(this, VideoEditActivity.class);
        intent.putExtra(TCConstants.VIDEO_EDITER_IMPORT, false);
        intent.putExtra(TCConstants.VIDEO_EDITER_PATH,mvideoPath );
        startActivity(intent);
        finish();
    }
}
